package com.yuncheng.spcyApi.flowService;

import com.yuncheng.vo.GetKsKz;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.busService.BusService;
import com.yuncheng.spcyApi.busService.SpcyDaService;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.flow.DzdaJysqFlowConstant;
import com.yuncheng.spcyApi.constant.flow.YlqxFlowConstant;
import com.yuncheng.spcyApi.constant.other.DaConstant;
import com.yuncheng.spcyApi.entity.SpcyDaSqlc;
import com.yuncheng.spcyApi.entity.SpcySqsxJob;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.exception.BusinessException;

import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcySqsxJobService;
import com.yuncheng.spcyApi.service.ISysUserFgldService;
import com.yuncheng.spcyApi.service.impl.YcSysUserServiceImpl;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.da.DaFlowVo;
import com.yuncheng.spcyApi.vo.flow.AgentVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.util.*;

@Component(value = "dzdaJysqFlowService")
public class DzdaJysqFlowService extends FlowService {
    private static final Logger log = LoggerFactory.getLogger(DzdaJysqFlowService.class);

    @Resource
    @Lazy
    private DzdaJysqFlowConstant flowConstant;

    @Resource
    @Lazy
    private ISpcySqsxJobService spcySqsxJobService;

    @Resource
    @Lazy
    private ISysUserFgldService sysUserFgldService;

    @Resource
    @Lazy
    private YcSysUserServiceImpl ycSysUserService;

    @Resource
    @Lazy
    private BusService busService;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Resource
    @Lazy
    private SpcyDaService spcyDaService;

    public Result handleFlow(DaFlowVo flowVo, SpcySqsxJob nowJob, String jobId, String sqid, String submitType, String opinion){
        // 当前流程任务
        if (StringUtils.isNotBlank(jobId)) {
            nowJob = spcySqsxJobService.getById(jobId);
        }

        if (nowJob == null){
            return Result.error("当前流程任务不存在，请重试");
        }

        sqid = nowJob.getFpid(); // 申请id

        // 提交按钮
        if (submitType.equals(DzdaJysqFlowConstant.SUBMIT_TYPE_SUBMIT)) {
            if (nowJob == null) {
                return Result.error("当前流程任务不存在");
            }
        }
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return Result.error("登录已失效，请重新登录");
        }

        if (nowJob.getFhjmc().equals(DzdaJysqFlowConstant.HJMC_SQ)){
            // 流程环节处理
            Result httpResult = handleNodeName(flowVo, submitType, nowJob, sqid, opinion);
            if (!httpResult.isSuccess()) {
                return httpResult;
            }else if (submitType.equals(DzdaJysqFlowConstant.SUBMIT_TYPE_SUBMIT)){
                spcyDaService.updateJysqStateByDzda(sqid,null, DaConstant.SQ_SPZT_DSP);
            }
        } else if (nowJob.getFhjmc().equals(DzdaJysqFlowConstant.HJMC_DSP)){
            // 流程环节处理
            Result httpResult = handleNodeName(flowVo, submitType, nowJob, sqid, opinion);
            if (!httpResult.isSuccess()) {
                return httpResult;
            }else if (submitType.equals(DzdaJysqFlowConstant.SUBMIT_TYPE_SUBMIT)){
                spcyDaService.updateJysqStateByDzda(sqid,null, DaConstant.SQ_SPZT_YTG);
            }
        }else {
            // 流程环节处理
            Result httpResult = handleNodeName(flowVo, submitType, nowJob, sqid, opinion);
            if (!httpResult.isSuccess()) {
                return httpResult;
            }
        }

        return Result.ok("操作成功");
    }

    public Result handleNodeName(DaFlowVo flowVo,String submitType, SpcySqsxJob job, String sqid, String opinion){
        HashMap<String, Object> map = flowVo.getMap();
        String dbrid = flowVo.getDbrid();// 下环节待办人
        String smgflag = flowVo.getSmgflag(); // 短信发送标志
        boolean isSubmit = true; // 是否提交流程
        Result res = null;

        // 保存按钮
        if (submitType.equals(DzdaJysqFlowConstant.SUBMIT_TYPE_SAVE)){
            return Result.ok("保存成功");
        }

        // 提交按钮
        if (submitType.equals(DzdaJysqFlowConstant.SUBMIT_TYPE_SUBMIT)){

            // 流程任务id
            String jobId = "";
            if (job != null){
                jobId = job.getId();
            }

            // 提交流程任务,待办人是否为空，指定待办人
            if (isSubmit) {
                if (StringUtils.isBlank(dbrid)) {
                    // 提交-流程任务
                    res = this.passJob(sqid, jobId, opinion, "", "");
                } else {
                    // 提交-流程任务
                    res = this.passJob(sqid, jobId, opinion, "", dbrid);
                }
            }
            if (!res.isSuccess()){
                return res;
            }else {
                SpcySqsxJob nowJob = (SpcySqsxJob)res.getResult();
                // 接收返回的流程任务，如果返回的流程任务id不相等，发送短信
                if (nowJob != null && !jobId.equals(nowJob.getId()) && StringUtils.isNotBlank(smgflag) && smgflag.equals("是")){
                    busService.sendSmsToPassJob(nowJob);
                }
            }
        }

        return Result.ok("提交成功");
    }

    /**
     * 查询借阅流程详情-通过申请id
     */
    public HttpResult querySqlcJobBySqid(String sqid){
        Map resultMap = new HashMap();


        return HttpResult.ok(resultMap);
    }

    /**
     * 借出-借阅档案
     */
    public HttpResult jcjyda(String sqid){
        return spcyDaService.jcjyda(sqid);
    }

    /**
     * 归还-借阅档案
     * @param sqid
     * @param isGly 是否管理员
     * @return
     */
    public HttpResult ghDzJysq(String sqid,boolean isGly){
        return spcyDaService.ghDzJysq(sqid, isGly);
    }

    /**
     * 确认归还-借阅档案
     * @param sqid
     * @return
     */
    public HttpResult qrghJyda(String sqid){
        return spcyDaService.qrghJyda(sqid);
    }

    /**
     * 初始化-借阅申请流程
     * @param isSubmit 是否提交（true/false）
     */
    public Result initJysqFlow(SpcyDaSqlc sqlc,boolean isSubmit){
        SpcyDaSqlc sqFlow = null;

        Result result = spcyDaService.addJysqByDzda(sqlc);
        if (!result.isSuccess()){
            return Result.error(result.getMessage());
        }else {
            sqFlow = (SpcyDaSqlc) result.getResult();
            String sqid = sqFlow.getId();

            // 初始化功能
            Result initResult = this.initJob(sqid, spcyDaService.getNowUser());
            if (!initResult.isSuccess()){
                return Result.error(initResult.getMessage());
            }else {
                if (isSubmit) {
                    SpcySqsxJob nowJob = (SpcySqsxJob) initResult.getResult();

                    DaFlowVo flowVo = new DaFlowVo();
                    String submitType = DzdaJysqFlowConstant.SUBMIT_TYPE_SUBMIT;
                    String opinion = "申请借阅。";
                    Result result1 = this.handleNodeName(flowVo, submitType, nowJob, sqid, opinion);
                    if (!result1.isSuccess()){
                        return result1;
                    }
                }
            }
        }

        Result result1 = new Result(true);
        result1.setMessage("申请借阅成功");
        if (isSubmit){
            result1.setMessage("提交申请借阅成功");
        }
        result1.setResult(sqFlow);
        return result1;
    }

    /**
     * 取消-借阅申请流程
     */
    public HttpResult cancelJysqFlow(String jobids){
        if (StringUtils.isBlank(jobids)){
            return HttpResult.error("流程参数丢失，请联系管理员！");
        }

        List<String> jobidList = Arrays.asList(jobids.split(",|，"));
        if (CollectionUtils.isEmpty(jobidList)){
            return HttpResult.error("流程参数丢失，请联系管理员！");
        }

        List<SpcySqsxJob> jobList = spcySqsxJobService.listByIds(jobidList);
        if (CollectionUtils.isEmpty(jobList)){
            return HttpResult.error("流程数据不存在，请刷新重试并联系管理员！");
        }

        for (SpcySqsxJob job : jobList){
            spcySqsxJobService.removeById(job.getId());
            spcyDaService.deleteJysqFlow(job.getFpid());
        }

        return HttpResult.ok("取消成功");
    }

    /**
     * 撤回流程
     * @param jobid
     * @param opinion
     * @return
     */
    public Result returnFlow(String jobid,String opinion){
        if (StringUtils.isBlank(jobid)){
            return Result.error("参数丢失，请联系管理员");
        }
        SpcySqsxJob nowJob = spcySqsxJobService.getById(jobid);
        if (nowJob == null){
            return Result.error("当前流程任务不存在，请刷新重试");
        }
        if (StringUtils.isNotBlank(nowJob.getFblzt()) && nowJob.getFblzt().equals(SpcyConstant.BLZT_YB)){
            return Result.error("当前环节【"+nowJob.getFhjmc()+"】已经办理，无法撤回");
        }

        String nowNode = nowJob.getFhjmc(); // 当前环节
        String previousName = flowConstant.getPreviousName(nowNode); // 上环节名称

        StringBuffer logNr = new StringBuffer("撤回流程：\n");
        // 判断是否为第一个环节
        if (nowNode.equals(previousName)){
            spcySqsxJobService.removeById(jobid);
            logNr.append("环节名称").append("[").append(nowNode).append("]。");
        } else {
            // 上环节
            String fsyhjid = nowJob.getFsyhjid();
            if (StringUtils.isNotBlank(fsyhjid)) {
                SpcySqsxJob preJob = spcySqsxJobService.getById(fsyhjid);
                if (preJob != null) {
                    preJob.setFblzt(SpcyConstant.BLZT_DB);
                    preJob.setFthyj(opinion);
                    spcySqsxJobService.updateById(preJob);
                }
            }

            spcySqsxJobService.removeById(jobid);
            logNr.append("环节名称").append("[").append(nowNode).append("]");
        }
        if (StringUtils.isNotBlank(opinion)){
            logNr.append("\n").append("撤回意见").append("[").append(opinion).append("]。");
        }

        logNr.append("\n").append(LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(nowJob)));

        spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM+nowNode,logNr.toString(),nowJob.getFpid(),SpcyConstant.LOGS_SYS_TYPE_TH,SpcyConstant.LOGS_SYS_TYPE_TH);

        return Result.ok("撤回成功");
    }

    /**
     * 终止流程
     */
    public Result endFlowJob(String jobid){
        if (StringUtils.isBlank(jobid)){
            return Result.error("流程参数为空");
        }
        SpcySqsxJob job = spcySqsxJobService.getById(jobid);
        if (job == null){
            return Result.error("当前流程记录不存在，请刷新重试");
        }
        if (StringUtils.isNotBlank(job.getFblzt()) && job.getFblzt().equals(SpcyConstant.BLZT_YB)){
            return Result.error("当前流程记录已经结束");
        }
        job.setFblzt(SpcyConstant.BLZT_YB); // 已办
        boolean flag = spcySqsxJobService.updateById(job);
        if (flag) {
            String log = "终止流程-环节名称-[" + job.getFhjmc() + "]。";
            spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM+job.getFhjmc(), log, job.getFpid(), SpcyConstant.LOGS_SYS_TYPE_TJ, SpcyConstant.LOGS_SYS_TYPE_TJ);
        }

        return Result.ok("终止成功");
    }

    /**
     * 获取当前流程任务相关信息
     * @param jobid 流程任务id
     * @param sqid 申请id
     */
    public Map getJobConcernByJobId(String jobid,String sqid){
        Map map = new HashMap();

        SpcySqsxJob nowJob = null;

        if (StringUtils.isBlank(sqid)) {
            // 当前流转记录
            nowJob = spcySqsxJobService.getById(jobid);
            if (nowJob == null) {
                log.error("当前流程任务id为【{}】的流转记录不存在", jobid);
                return map;
            }

            sqid = nowJob.getFpid();

            if (org.apache.commons.lang3.StringUtils.isBlank(sqid)) {
                log.error("当前流程任务id为【{}】的流转记录中的申请id为空", jobid);
                return map;
            }
        }else {
            // 当前流程任务
            nowJob = this.getNowJobBySqid(sqid);

            if (nowJob == null) {
                log.error("当前流程任务id为【{}】的流转记录不存在", jobid);
                return map;
            }
        }

        // 申请信息
        SpcyDaSqlc sqxx = spcyDaService.querySqlcBySqid(sqid);

        // 流程任务流传记录
        List<SpcySqsxJob> jobList = this.getJobListBySqId(sqid);

        // 获取下环节待办人
        String nextNode = flowConstant.getNextNodeName(nowJob.getFhjmc());
        Map nextDbr = this.getNodeDbr(sqid, nextNode,"");
        List<AgentVo> dbrListByMap = busService.getDbrListByMap(nextDbr);

        map.put("sqxx",sqxx); // 申请信息
        map.put("job",nowJob); // 当前流转记录
        map.put("jobHistoryList",jobList); // 流程任务流传记录
        map.put("nextNodeDbrList",dbrListByMap); // 下环节待办人list
        map.put("nextNode",nextNode);

        return map;
    }

    /**
     * 获取当前申请所在流程环节 - 根据申请id
     */
    public SpcySqsxJob getNowJobBySqid(String sqid){

        // 获取申请-待办流程任务
        List<SpcySqsxJob> jobList = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFpid, sqid)
                .like(SpcySqsxJob::getFlcmc, flowConstant.getFlowName())
                .and(f -> f.eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_DB)
                        .or()
                        .eq(SpcySqsxJob::getFblzt, SpcyConstant.BLZT_TH)
                )
                .orderByDesc(SpcySqsxJob::getFfssj)
        );
        if (CollectionUtils.isNotEmpty(jobList)){
            return jobList.get(0);

        }else {
            return new SpcySqsxJob();
        }

    }

    /**
     * 获取流转记录 - 根据申请id
     */
    public List<SpcySqsxJob> getJobListBySqId(String sqid){
        LambdaQueryWrapper<SpcySqsxJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SpcySqsxJob::getFlcmc, flowConstant.getFlowName());
        queryWrapper.eq(SpcySqsxJob::getFpid, sqid);
        queryWrapper.orderByAsc(SpcySqsxJob::getFfssj);
        queryWrapper.orderByAsc(SpcySqsxJob::getCreateTime);
        List<SpcySqsxJob> jobList = spcySqsxJobService.list(queryWrapper);

        return jobList;
    }

    //初始化任务
    public Result initJob(String sqid, CurrentUserVo currentUser){
        String nodeName = flowConstant.getFirstNode();
        //需要校验是否已经初始化过
        List<SpcySqsxJob> list = spcySqsxJobService.list(new LambdaQueryWrapper<SpcySqsxJob>()
                .eq(SpcySqsxJob::getFpid, sqid)
                .eq(SpcySqsxJob::getFhjmc, nodeName)
                .eq(SpcySqsxJob::getFlcmc, flowConstant.getFlowName())
        );

        if(list.size()>0){
            return Result.ok(list.get(0));
        }
        return this.createNextJob(flowConstant,sqid,null,currentUser,nodeName,"");
    }

    /**
     * 提交当前环节的待办记录
     *  @param sqsxId 申请事项id
     *  @param jobId 当前环节id
     *  @param opinion 办理意见
     */
    public Result passJob(String sqsxId, String jobId, String opinion,String nextNodeName,String nextNodeDbrIds){
        //处理业务表的逻辑
        Result httpResult = this.passJob(flowConstant, sqsxId, jobId, opinion, nextNodeName, nextNodeDbrIds);
        return httpResult;
    }

    /**
     *
     * @param job
     * @param currentUser
     */
    private Result doJob(SpcySqsxJob job, CurrentUserVo currentUser, String opinion){
        String blrId = "";
        String blr = "";
        if (currentUser != null) {
            blrId = currentUser.getId();
            blr = currentUser.getName();
        }
        if (job == null){
            return Result.error("当前流程任务不存在，请重试");
        }
        if(SpcyConstant.BLZT_YB.equals(job.getFblzt())){
            return Result.error("当前任务已提交");
        }
        if(StringUtils.isBlank(job.getFdbrid())){
            return Result.error("当前任务未指定办理人");
        }
        if(!job.getFdbrid().contains(blrId)){
            return Result.error("当前登录用户无此任务的办理权限");
        }
        // 办理方式 竞争办理 、多人线性办理 、多人并行办理
        String blfs = job.getFblfs();
        if(StringUtils.isBlank(blfs)||blfs.equals(SpcyConstant.BLFS_DRJJ)){
            job.setFblr(blr);
            job.setFblsj(new Date());
            job.setFblyj(opinion);
            job.setFblzt(SpcyConstant.BLZT_YB);
            spcySqsxJobService.updateById(job);
            return Result.ok(job);
        }else if(blfs.equals(SpcyConstant.BLFS_DRXX)  || blfs.equals(SpcyConstant.BLFS_DRBX)){
            //先根据多个人线性和多人并行的逻辑获得下一办理人
            String nextBlrId ="";
            String nextBlr ="";
            if(blfs.equals(SpcyConstant.BLFS_DRXX)){
                String ydbrIds[] = job.getFydbrid().split(",");
                String ydbrs[] = job.getFydbr().split(",");
                for (int i=0;i<ydbrIds.length;i++){
                    String dbrId = ydbrIds[i];
                    //不考虑办理人id重复出现（一个人办多次）的情况
                    if(dbrId.equals(blrId)){
                        if(i+1<ydbrIds.length){
                            nextBlrId=ydbrIds[i+1];
                            nextBlr=ydbrs[i+1];
                            break;
                        }
                    }
                }
            }else{
                String dbrIds[] = job.getFdbrid().split(",");
                String dbrs[] = job.getFdbr().split(",");
                for (int i=0;i<dbrIds.length;i++){
                    String dbrId = dbrIds[i];
                    String dbr = dbrs[i];
                    //不考虑办理人id重复出现（一个人办多次）的情况
                    if(!dbrId.equals(blrId)){
                        nextBlrId=nextBlrId+(StringUtils.isBlank(nextBlrId)?"":",")+dbrId;
                        nextBlr=nextBlr+(StringUtils.isBlank(nextBlr)?"":",")+dbr;
                    }
                }
            }

            //如果存在下一办理人 就继续待办，多人办理意见用@@分隔，办理时间、办理人、意见 用|分隔
            job.setFblr(blr);
            job.setFblsj(new Date());
            String blyj=(job.getFblyj()==null?"":job.getFblyj())+(StringUtils.isBlank(job.getFblyj())?"":"@@")+ DateConversionUtils.DateToString(new Date()) +"|"+blr+"|"+ opinion;
            job.setFblyj(blyj);
            if(StringUtils.isBlank(nextBlrId)){
                job.setFblzt(SpcyConstant.BLZT_YB);
            }else{
                job.setFblzt(SpcyConstant.BLZT_DB);
                job.setFdbrid(nextBlrId);
                job.setFdbr(nextBlr);
            }
            spcySqsxJobService.updateById(job);
            return Result.ok(job);
        }
        return Result.error("待办任务未指定办理方式");
    }

    /**
     *
     * @param sqsxId
     * @param currentJob
     * @param currentUser
     * @param nextNodeName
     * @param nextNodeDbrIds
     * @return
     */
    public Result createNextJob(String sqsxId, SpcySqsxJob currentJob, CurrentUserVo currentUser, String nextNodeName, String nextNodeDbrIds){
        if (currentJob != null) {
            nextNodeName = getNextNodeName(currentJob.getFhjmc(), nextNodeName);
        }else {
            if (StringUtils.isBlank(nextNodeName)){
                return Result.error("下环节不能为空");
            }
        }
        if(nextNodeName.equals(YlqxFlowConstant.HJMC_LCJS)){
            SpcySqsxJob job = new SpcySqsxJob();

            return Result.ok("操作成功");
        }
        Map map = this.getNodeDbr(sqsxId,nextNodeName,nextNodeDbrIds);
        nextNodeDbrIds = map.get("dbrId").toString();
        String nextNodeDbrs = map.get("dbr").toString();
        if(StringUtils.isBlank(nextNodeDbrIds)){
            return Result.error("获取不到下一环节待办人");
        }
        SpcySqsxJob job = new SpcySqsxJob();
        job.setId(GetUuIdUtils.NotReplaceUuId());
        if(currentJob != null){
            job.setFsyhjid(currentJob.getId());
        }
        job.setFpid(sqsxId);
        job.setFhjmc(nextNodeName);
        if (currentUser != null) {
            job.setFfsrid(currentUser.getId());
            job.setFfsrmc(currentUser.getName());
        }
        job.setFfssj(new Date());

        job.setFdbrid(nextNodeDbrIds);
        job.setFdbr(nextNodeDbrs);
        job.setFydbrid(nextNodeDbrIds);
        job.setFydbr(nextNodeDbrs);

        // job.setFblfs(YlqxFlowConstant.getBlfs(nextNodeName));
        job.setFblzt(SpcyConstant.BLZT_DB);
        spcySqsxJobService.save(job);
        return Result.ok(job);
    }

    /**
     *
     * @param currentNodeName 可为空(如果为空，nextNodeName不为空)
     * @param nextNodeName 可为空(如果为空，currentNodename不为空)
     * @return
     */
    private String getNextNodeName(String  currentNodeName,String nextNodeName){
        if (StringUtils.isNotBlank(nextNodeName)){
            return nextNodeName;
        }else{
            return flowConstant.getNextNodeName(currentNodeName);
        }
    }

    // 获取当前环节待办人
    public Map getNodeDbr(String sqid,String nodeName, String dbrIds){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        if(StringUtils.isNotBlank(dbrIds)){
            dbr = getDbrsByIds(dbrIds);
            dbrId = dbrIds;
        }else{

            if (nodeName.equals(DzdaJysqFlowConstant.HJMC_SQ)){
                if (StringUtils.isBlank(sqid)){
                    throw new BusinessException("申请参数丢失，请联系管理员!");
                }
                SpcyDaSqlc sqlc = spcyDaService.querySqlcBySqid(sqid);
                if (sqlc == null){
                    throw new BusinessException("申请数据不存在，请刷新重试并联系管理员！");
                }

                dbrId = sqlc.getFsqrid();
                dbr = sqlc.getFsqrmc();
            }

            if (nodeName.equals(DzdaJysqFlowConstant.HJMC_DSP)) {
                Map kzMap = getDagly();
                dbrId = kzMap.get("dbrId").toString();
                dbr = kzMap.get("dbr").toString();
            }

        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 获取当前环节待办人
    public Map getNodeDbr(String nodeName, String dbrIds){
        return this.getNodeDbr(null,nodeName,dbrIds);
    }

    // 根据待办人id，获取名称拼接起来
    private String getDbrsByIds(String dbrIds){
        //把待办人按id查出来  把名字拼起来
        String dbr = "";
        List<String> dbrIdsList = Arrays.asList(dbrIds.split(","));
        List<YcSysUser> sysUserList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dbrIdsList)){
            sysUserList = ycSysUserService.listByIds(dbrIdsList);
        }

        if (CollectionUtils.isNotEmpty(sysUserList)){
            for (YcSysUser user : sysUserList){
                dbr += user.getRealname() + ",";
            }
        }

        return dbr;
    }

    // 对应科室的 科室负责人
    public Map getKsfzrByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, SpcyConstant.GW_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId()+",";
                dbr += ksKz.getRealname()+",";
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 获取对应科室的 分管领导
    public Map getKsFgldByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        YcSysUser fgldUserBydept = sysUserFgldService.getFgldUserBydept(deptName);
        if (fgldUserBydept != null) {
            dbrId = fgldUserBydept.getId();
            dbr = fgldUserBydept.getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }

    // 档案管理人
    public Map getDagly(){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(SpcyConstant.DEPT_NAME_BGS, SpcyConstant.DA_BGS_FZR);

        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId()+",";
                dbr += ksKz.getRealname()+",";
            }
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }


}
